package observability

import (
	"context"
	"time"

	"github.com/diggerhq/digger/opentaco/internal/domain"
	"github.com/diggerhq/digger/opentaco/internal/query"
)

// SyncHealthChecker monitors the health of blob/query synchronization
type SyncHealthChecker struct {
	repo  domain.UnitRepository
	query query.Store
}

// NewSyncHealthChecker creates a health checker for sync status
func NewSyncHealthChecker(repo domain.UnitRepository, query query.Store) *SyncHealthChecker {
	return &SyncHealthChecker{
		repo:  repo,
		query: query,
	}
}

// SyncHealthStatus represents the sync health status
type SyncHealthStatus struct {
	Healthy       bool      `json:"healthy"`
	BlobUnits     int       `json:"blob_units"`
	QueryUnits    int       `json:"query_units"`
	SyncDrift     int       `json:"sync_drift"`
	LastChecked   time.Time `json:"last_checked"`
	Message       string    `json:"message,omitempty"`
}

// CheckSyncHealth verifies that blob and query are in sync.
// This also serves as a database health check - the database is critical for:
// - Fast unit listing (query index)
// - RBAC enforcement (permissions, roles, user assignments)
// If the database is unavailable, RBAC will fail closed (deny all access).
func (h *SyncHealthChecker) CheckSyncHealth(ctx context.Context) *SyncHealthStatus {
	status := &SyncHealthStatus{
		LastChecked: time.Now(),
		Healthy:     true,
	}

	// Get units from repository (uses query index)
	// This also checks database connectivity
	// Use default org for health check
	queryUnits, err := h.repo.List(ctx, "default", "")
	if err != nil {
		status.Healthy = false
		status.Message = "Database unavailable (critical for RBAC and fast listing): " + err.Error()
		return status
	}
	status.QueryUnits = len(queryUnits)

	// For now, assume blob and query are in sync
	// Full drift detection would require direct blob access, which breaks encapsulation
	// The sync retry logic should keep them in sync
	status.BlobUnits = status.QueryUnits
	status.SyncDrift = 0

	if status.SyncDrift > 0 {
		status.Message = "Sync drift detected - query index may be out of sync with blob storage"
	}

	return status
}
